package com.sixth.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.InventoryDao;
import com.sixth.dao.OutlibraryDao;
import com.sixth.entity.Goods;
import com.sixth.entity.Inventory;
import com.sixth.entity.InventoryGoods;
import com.sixth.entity.Staff;
import com.sixth.service.InventoryService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import com.sixth.util.TableLayUi;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.sixth.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project01
 * @description:
 * @author: 高振杨
 * @create: 2021-12-21 11:24
 **/
@Service(value = "inventoryservice")
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryDao dao;
    @Autowired
    private OutlibraryDao outLibrary;

    //查询盘点记录表
    public TableLayUi getAllInventory(int page, int limit,Integer branchid) {
        PageHelper.startPage(page,limit);
        List<Inventory> list=dao.getAllInventory(branchid);
        PageInfo<Inventory> pageInfo = new PageInfo<Inventory>(list);
        return  new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }
    //根据当前盘点编号invenid获取到报损详情表内相应的信息
    public TableLayUi getInventoryGoods(int page, int limit,Integer invenid) {
        PageHelper.startPage(page,limit);
        List<Map> list = dao.getInventoryGoods(invenid);
        PageInfo<Map> pageInfo = new PageInfo<Map>(list);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    //审核通过
    public CommonResult inventoryExPass(Integer invenid, Integer branchid) {
        //先修改审核状态
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer i = dao.updateInventoryExPass(invenid, new Date(), staffid);
        //判断审核状态是否更改成功
        if (i > 0) {
            //根据盘点编号查询盘点表单
            List<InventoryGoods> inventoryByInvenid = dao.getInventoryByInvenid(invenid);
            //循环将损坏的从仓库删除
            for (InventoryGoods inventoryGoods : inventoryByInvenid) {
                //获得商品id
                Integer goodsid = inventoryGoods.getGoodsid();
                //获得损坏数量
                Integer lossnum = inventoryGoods.getLossnum();


                //根据店铺id 商品id 和数量删除
                outLibrary.batchOutLibrary(branchid, goodsid, lossnum);
            }
            return new CommonResult(1,"操作成功！",null);
        }
        return new CommonResult(2,"操作失败",null);
    }

    //审核驳回
    public CommonResult inventoryExReject(Integer invenid,Integer branchid,  String remarks) {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer integer = dao.updateInventoryExReject(invenid, new Date(), staffid, remarks);
        CommonResult result;
        if (integer > 0) {
            result = new CommonResult(1,"驳回成功！",null);
        }else {
            result = new CommonResult(2,"驳回失败",null);
        }
        return result;
    }
    //查询是否已有盘点
    public CommonResult selectPanDian(Integer branchid) {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer branchid1 = staff.getBranchid();
        List<Inventory> list=dao.selectInventoryByBranchid(branchid1);
        CommonResult commonResult;
        if (list.size() > 0) {
            commonResult = new CommonResult(2,"操作失败",null);
        } else {
            commonResult = new CommonResult(1,"操作成功",null);
        }
        return commonResult;
    }

    //新增盘点
    public CommonResult addpanDian(String panDianGoods) {
        //将发送的json数据集合到Goods中
        List<Goods> goods = JSONArray.parseArray(panDianGoods, Goods.class);
        //获取当前登陆人及其id号码
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");

        Date date = new Date();

        Integer staffid = staff.getStaffid();
        Integer branchid = staff.getBranchid();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dates = format.format(date);

        Integer i = dao.addInventoryOne(dates,staffid,branchid);

        Inventory inventory = dao.selectInvenidByDate(dates);

        Integer invenid =inventory.getInvenid();

        System.out.println(invenid);

        //判断是否添加审核成功
        if (i > 0) {
            //循环存入批量盘盘点的商品详细信息
            for (Goods good : goods) {
                //InventoryGoods ig = new InventoryGoods();
                Integer goodsid = good.getGoodsid();
                //根据实体类存入的属性获取报损数量和盘点数量
                Integer panDianNum = good.getPanDianNum();
                Integer baoSunNum = good.getBaoSunNum();
               Integer j= dao.addInventoryGoods(invenid,goodsid,panDianNum,baoSunNum);

                if (j == null) {
                    return new CommonResult(2, "操作失败", null);
                }
            }
            return new CommonResult(1, "操作成功", null);
        }
        return new CommonResult(2, "操作失败", null);
    }

    //获取全部报损信息
    public TableLayUi getAllFrmLoss(int page, int limit, Integer branchid, String time) {
        PageHelper.startPage(page,limit);
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        Integer bfbranchid = staff.getBranchid();
        Integer roleid = staff.getRoleid();
        String starttime = null;
        String endtime = null;
        if (time!=null&&time!="") {
            starttime = time.substring(0,19);
            endtime = time.substring(22,time.length());
        }
        List<Inventory> allLoss = dao.getAllFrmLoss(branchid, starttime, endtime, bfbranchid, roleid);
        PageInfo<Inventory> pageInfo = new PageInfo<Inventory>(allLoss);
        return new TableLayUi(0,"成功",pageInfo.getTotal(),pageInfo.getList());
    }


}